package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.AreaEntity;
import com.ly.heart.mapper.AreaMapper;
import com.ly.heart.service.IAreaService;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaEntity> implements IAreaService {

    @Resource
    private AreaMapper areaMapper;


    @Override
    public List<AreaEntity> getList (AreaEntity areaEntity) {
        List<AreaEntity> areaEntities = areaMapper.selectAreaEntityList ( areaEntity );
        return areaEntities;
    }

    @Override
    public AjaxResult queryList (AreaEntity areaEntity) {
        LambdaQueryWrapper<AreaEntity> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        if ( StringUtils.isNotBlank ( areaEntity.getParentId () ) ) {
            lambdaQueryWrapper.eq ( AreaEntity :: getParentId, areaEntity.getParentId () );
        }
        lambdaQueryWrapper.eq ( AreaEntity :: getIsUsed, ConstUtil.IS_USED_YES );
        Page<AreaEntity> page = this.page ( new Page<> ( areaEntity.getPage (), areaEntity.getLimit () ), lambdaQueryWrapper );
        return AjaxResult.success (page);
    }

    @Override
    public int saveSingle (AreaEntity areaEntity) {
        areaEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        areaEntity.setCreateTime ( new Date () );
        int insert = areaMapper.insert ( areaEntity );
        if ( insert == 0 ) {
            throw new RuntimeException ( "新增地区失败" );
        }
        return insert;
    }

}
